import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../components/login.vue' // 路由懒加载
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/login.vue')
// import Home from '../components/home.vue'
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/home.vue')
// import Welcome from '../components/welcome.vue'
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/welcome.vue')
// import Users from '../components/user/users.vue'
const Users = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/user/users.vue')
// import Rights from '../components/power/rights.vue'
const Rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/rights.vue')
// import Roles from '../components/power/roles.vue'
const Roles = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/roles.vue')
// import GoodList from '../components/goods/list.vue'
const GoodList = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/list.vue')
// import GoodParams from '../components/goods/params.vue'
const GoodParams = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/params.vue')
// import GoodCate from '../components/goods/cate.vue'
const GoodCate = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/cate.vue')
// import Order from '../components/order/order.vue'
const Order = () => import(/* webpackChunkName: "Order_Report" */ '../components/order/order.vue')
// import Report from '../components/report/report.vue'
const Report = () => import(/* webpackChunkName: "Order_Report" */ '../components/report/report.vue')
// import AddGood from '../components/goods/add.vue'
const AddGood = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/add.vue')
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome, props: route => ({ username: route.params.username }) },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/goods', component: GoodList },
      { path: '/goods/add', component: AddGood },
      { path: '/params', component: GoodParams },
      { path: '/categories', component: GoodCate },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  // 登陆直接放行
  // to 将要访问的路径
  // from 代表从那个路径跳转过来
  // next 表示放行
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
